<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">

    <title>Insert title here</title>
    <link rel="stylesheet" type="text/css" media="screen" href="../../css/bootstrap.min.css">
    <link rel="stylesheet" type="text/css" media="screen" href="../../css/dataTables.bootstrap.min.css">
    <link rel="stylesheet" type="text/css" media="screen" href="../../layui/css/layui.css">
    <link rel="stylesheet" href="../../css/global.css" media="all"/>
</head>
<body>
<div class="col-xs-12 col-sm-12 col-md-12 col-lg-12 page-container">
    <header class="form-filter-wrap">
        <form class="layui-form form-inline" onsubmit="return false">
            <div class="form-group">
                <div class="form-filter">
                    <div class="form-inner">
                        <label class="layui-form-label">描述：</label>
                        <div class="layui-input-inline" style="width: 180px;">
                            <input id="description" type="text" style="width: 170px;" class="form-control"
                                   placeholder="描述">
                        </div>
                    </div>
                    <div class="form-inner">
                        <label class="layui-form-label" style="width: 80px;">状态：</label>
                        <div class="layui-input-inline" style="width: 80px;">
                            <select class="form-control input-sm" id="status">
                                <option value="">全部</option>
                                <option value="1">正常</option>
                                <option value="0">取消</option>
                            </select>
                        </div>
                    </div>
                    <button id="searchBt" class="layui-btn layui-btn-sm" permission="job:query"><i
                            class="layui-icon">&#xe615;</i>搜索
                    </button>
                </div>
                <div class="form-btn">
                    <button class="layui-btn layui-btn-sm" onclick="location.href='addJob.html'"
                            permission="job:add">
                        <i class="layui-icon">&#xe608;</i> 添加
                    </button>
                </div>
            </div>
        </form>
    </header>

    <div class="content-container">
        <div class="widget-body no-padding">
            <table id="dt-table" class="table table-striped table-bordered table-hover">
                <thead>
                <tr>
                    <th>id</th>
                    <th>定时任务名称</th>
                    <th>cron表达式</th>
                    <th>bean名</th>
                    <th>方法名</th>
                    <th>描述</th>
                    <th>状态</th>
                    <th>修改时间</th>
                    <th>操作</th>
                </tr>
                </thead>
                <tbody>
                </tbody>
            </table>
        </div>
    </div>
</div>
</body>
</html>

<script type="text/javascript" src="../../js/libs/jquery-1.11.1.min.js"></script>
<script type="text/javascript" src="../../js/jq.js"></script>
<script type="text/javascript" src="../../js/plugin/datatables/jquery.dataTables.min.js"></script>
<script type="text/javascript" src="../../js/plugin/datatables/dataTables.bootstrap.min.js"></script>
<script type="text/javascript" src="../../layui/layui.js"></script>
<script type="text/javascript" src="../../js/my/permission.js"></script>
<script type="text/javascript">
    layui.use('layer', function () {
        var layer = layui.layer;
    });
    var pers = checkPermission();

    var example;

    function init() {
        example = $('#dt-table').DataTable({
            "searching": false,
            "processing": false,
            "serverSide": true,
            "ordering": false,
            "language": {
                "url": "/js/plugin/datatables/Chinese.lang"
            },
            "ajax": {
                "url": "/jobs",
                "type": "get",
                "data": function (d) {
                    d.description = $('#description').val();
                    d.status = $('#status').val();
                },
                "error": function (xhr, textStatus, errorThrown) {
                    var msg = xhr.responseText;
                    console.log(msg);
                    var response = JSON.parse(msg);
                    var code = response.code;
                    var message = response.message;
                    if (code == 400) {
                        layer.msg(message);
                    } else if (code == 401) {
                        localStorage.removeItem("token");
                        layer.msg("token过期，请先登录", {shift: -1, time: 1000}, function () {
                            location.href = '/login.html';
                        });
                    } else if (code == 403) {
                        console.log("未授权:" + message);
                        layer.msg('未授权');
                    } else if (code == 500) {
                        layer.msg('系统错误：' + message);
                    }
                }
            },
            "dom": "<'dt-toolbar'r>t<'dt-toolbar-footer'<'table-bottom col-sm-10 col-xs-12 hidden-xs'i><'table-bottom col-xs-12 col-sm-10' p v>>",
            "columns": [
                {"data": "id", "defaultContent": ""},
                {"data": "jobName", "defaultContent": ""},
                {"data": "cron", "defaultContent": ""},
                {"data": "springBeanName", "defaultContent": ""},
                {"data": "methodName", "defaultContent": ""},
                {"data": "description", "defaultContent": ""},
                {
                    "data": "status",
                    "defaultContent": "",
                    "render": function (data, type, row) {
                        var status = row['status'];
                        var a;
                        if (status == 1) {
                            a = "正常";
                        } else {
                            a = "<span style='color:red'>取消</span>";
                        }
                        return a;
                    }
                },
                {"data": "updateTime", "defaultContent": ""},
                {
                    "data": "",
                    "defaultContent": "",
                    "orderable": false,
                    "render": function (data, type, row) {
                        var isSysJob = row['isSysJob'];
                        if (!isSysJob) {
                            var id = row['id'];
                            var href = "updateJob.html?id=" + id;
                            var edit = buttonEdit(href, "job:add", pers);
                            var del = buttonDel(id, "job:del", pers);
                            return edit + del;
                        } else {
                            return "<span style='color:red'>系统任务无操作</span>";
                        }
                    }
                },
            ],
            "order": [[6, "desc"], [7, "desc"]]
        });
    }

    function del(id) {
        layer.confirm('确定要取消吗？', {title: ['取消', 'font-size: 20px;font-weight: bold;']}, function () {
            $.ajax({
                type: 'delete',
                url: '/jobs/' + id,
                success: function (data) {
                    example.ajax.reload();
                    layer.msg("取消成功");
                }
            });
            layer.close(1);
        });
    }

    $("#searchBt").click(function () {
        example.ajax.reload();
    });

    init();
</script>